#include "parallel.h"
#include "ui_parallel.h"
#include <QtWidgets/QPushButton>
#include <QtWidgets/QGridLayout>
#include <QMessageBox>
#include <QtWidgets/QDesktopWidget>
#define COLUMN_SPACING 8
Parallel::Parallel(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Parallel)
{
     SetMainBacklight();

    ui->setupUi(this);
    ycapi = new Ycapi();


    threadFlag=0;
}
void Parallel::SetMainBacklight()
{
QPalette bgpal = palette();
bgpal.setColor (QPalette::Background, QColor (0, 0 , 0, 255));
bgpal.setColor (QPalette::Foreground, QColor (255,0,0,255));
setPalette (bgpal);
}

Parallel::~Parallel()
{
    delete ui;
}

void Parallel::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void Parallel::on_setAllL_clicked()
{
    for(int i=0;i<4;i++)
        ycapi->SetIO(0,i);
}

void Parallel::on_setAllH_clicked()
{
    for(int i=0;i<4;i++)
        ycapi->SetIO(1,i);
}

void Parallel::on_setPolling_clicked()
{
    ycapi->SetIoMode(0xf,IO_POLLING_MODE,0);
}

void Parallel::on_readIn_clicked()
{
    unsigned char level[8]={0};
    for(int i=0;i<4;i++)
    {
        ycapi->GetIO(&level[i],i);
    }

    ui->in0->setText(QString::number(int(level[0])));
    ui->in1->setText(QString::number(int(level[1])));
    ui->in2->setText(QString::number(int(level[2])));
    ui->in3->setText(QString::number(int(level[3])));
}

void Parallel::on_setIntr_clicked()
{
      ycapi->SetIoMode(0xf,IO_INTR_MODE,IO_INTR_BOTH_EDGE_TRIGGERED);
}

void Parallel::on_readInBlock_clicked()
{
    m_readThread0 = new ReadThread(0);
    connect(m_readThread0,SIGNAL(ReceivedData(unsigned char *,unsigned char)),this,SLOT(DataAvailable(unsigned char *,unsigned char)));
    m_readThread0->start();

    m_readThread1 = new ReadThread(1);
    connect(m_readThread1,SIGNAL(ReceivedData(unsigned char *,unsigned char)),this,SLOT(DataAvailable(unsigned char *,unsigned char)));
    m_readThread1->start();

    m_readThread2 = new ReadThread(2);
    connect(m_readThread2,SIGNAL(ReceivedData(unsigned char *,unsigned char)),this,SLOT(DataAvailable(unsigned char *,unsigned char)));
    m_readThread2->start();

    m_readThread3 = new ReadThread(3);
    connect(m_readThread3,SIGNAL(ReceivedData(unsigned char *,unsigned char)),this,SLOT(DataAvailable(unsigned char *,unsigned char)));
    m_readThread3->start();
    threadFlag=1;
}

void Parallel::DataAvailable(unsigned char* level,unsigned char ioNum)
{
         printf("DataAvailable level:%d,ioNum:%d\r\n",level[0],ioNum);
         switch(ioNum)
         {
         case 0:
             ui->in00->setText(QString::number(int(level[0]>>0&0x01)));
             break;
         case 1:
             ui->in11->setText(QString::number(int(level[0]>>1&0x01)));
             break;
         case 2:
             ui->in22->setText(QString::number(int(level[0]>>2&0x01)));
             break;
         case 3:
             ui->in33->setText(QString::number(int(level[0]>>3&0x01)));
             break;
         }

}
void Parallel::closeEvent(QCloseEvent * event)
{
    if(threadFlag)
    {
    m_readThread0->flag=0;
    m_readThread1->flag=0;
     m_readThread2->flag=0;
     m_readThread3->flag=0;
     m_readThread0->quit();
     m_readThread1->quit();
     m_readThread2->quit();
     m_readThread3->quit();
    }
}

void Parallel::on_close_clicked()
{
    this->close();
}


